(function(){
    //食物的构造函数
    function Food (options){
        options = options || {};
        this.width = options.width || 20;
        this.height = options.height || 20;
        this.color = options.color || 'green';
        this.x = options.x  ||0;
        this.y = options.y  ||0;
    }
    //创建食物对象
    // var food = new Food();
    //将食物定义到外面，可以在任何位置访问
    var oldFood = null;
    //将食物渲染到页面上
    Food.prototype.render = function(map){
        removeFood(document.getElementById('map'),divFood);
        //获取格子的序数  x,y
        this.x = getRandom(0,document.getElementById('map').offsetWidth/20-1);
         this.y = getRandom(0,document.getElementById('map').offsetHeight/20-1);
        var divFood = document.createElement('div');
        divFood.style.width = this.width +'px';
        divFood.style.height = this.height +'px';
        divFood.style.backgroundColor = this.color;
        divFood.style.position = 'absolute';
        divFood.style.left = this.x*20 +'px';
        divFood.style.top = this.y*20 +'px';
        oldFood = divFood;
        map.appendChild(divFood);
    }
    //渲染之前删除已经有的食物，保证页面上只有一个食物
      function removeFood (map) {
        if(oldFood){
            map.removeChild(oldFood);
        }else{
            return false;
        }
       
    }
    window.Food = Food;
    //测试
    // food.render(document.getElementById('map'));
    // var timer = setInterval(function(){
    //     food.render(document.getElementById('map'));
    // },1000);
})();